/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			formatmanager.cc
  \date			Sep 2012
  \author		TNick

  \brief		Contains the implementation of FormatManager class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<cpg/cpg.h>

#include	<bytebuffer.h>
#include	<cpg/preferences/prefs.h>
#include	<cpg/file-formats/idrawfile.h>

#include	"formatmanager.h"


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace cpg;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

FormatManager *		FormatManager::uniq_ = NULL;

/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
FormatManager::FormatManager	( void )
	: MemTrack()
{
	Q_ASSERT( uniq_ == NULL );
	uniq_ = this;
	def_ = NULL;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
FormatManager::~FormatManager	( void )
{
	Q_ASSERT( uniq_ == this );
	uniq_ = NULL;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool			FormatManager::init				( void )
{
	if ( uniq_ != NULL )
		return true;

	new	FormatManager();

//	new BinDrawFile();
//	new SqlDrawFile();
//	new XmlDrawFile();
//	Q_ASSERT( PROTECTED_IDRAWFILE_CONTENT == uniq_->lst_.count() );

	QString s = Prefs::getS( Prefs::FILE_FORMAT_DEFAULT );
	IDrawFile * new_crt = instFromName( s );
	if ( new_crt != NULL )
		setDefaultInst( new_crt );

	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			FormatManager::end				( void )
{
	if ( uniq_ == NULL )
		return;

//	delete uniq_->lst_.at( 0 );
//	delete uniq_->lst_.at( 1 );
//	delete uniq_->lst_.at( 2 );

	delete uniq_;

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString			FormatManager::gofnExt			( void )
{
	QStringList	sl;
	foreach( IDrawFile * idf, uniq_->lst_ )
	{
		sl.append( idf->getExtDec() );
	}
	return sl.join( ";;" );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
IDrawFile *		FormatManager::instFromFile		(
		const QString & s_file_name )
{
	int i = s_file_name.lastIndexOf( "." );
	if ( i == -1 )
	{
		return NULL;
	}
	QString ext = s_file_name.mid( i+1 ).toUpper();
	foreach( IDrawFile * idf, uniq_->lst_ )
	{
		if ( idf->isThisYours( ext ) )
			return idf;
	}
	return NULL;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
IDrawFile *		FormatManager::instFromContent		(
		BBlocks::ByteBuffer & p_data )
{
	Q_ASSERT( p_data.isEmpty() == false );

	foreach( IDrawFile * idf, uniq_->lst_ )
	{
		if ( idf->isThisYours( p_data ) )
			return idf;
	}
	return NULL;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
IDrawFile *		FormatManager::instFromName		( const QString & s_name )
{
	if ( s_name.isEmpty() )
	{
		return NULL;
	}

	foreach( IDrawFile * idf, uniq_->lst_ )
	{
		if ( QString::compare( s_name, idf->name(), Qt::CaseInsensitive ) == 0 )
			return idf;
	}
	return NULL;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			FormatManager::setDefaultInst	( IDrawFile * new_val )
{
	int i = uniq_->lst_.indexOf( new_val );
	if ( i == -1 )
	{
		Q_ASSERT( false );
		return;
	}
	uniq_->def_ = new_val;

	if ( new_val != NULL )
	{
		Prefs::set( Prefs::FILE_FORMAT_DEFAULT, new_val->name() );
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			FormatManager::append			( IDrawFile * new_it )
{
	int i = uniq_->lst_.indexOf( new_it );
	if ( i != -1 )
	{
		Q_ASSERT( false );
	}
	else
	{
		uniq_->lst_.append( new_it );
		if ( uniq_->def_ == NULL )
		{
			uniq_->def_ = new_it;
		}
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			FormatManager::remove			( int i )
{
	if ( ( i < PROTECTED_IDRAWFILE_CONTENT ) || ( i >= uniq_->lst_.count() ) )
		return;

	if ( uniq_->def_ == uniq_->lst_.at( i ) )
	{
		if ( uniq_->lst_.count() == 1 )
		{
			uniq_->def_ = NULL;
		}
		else
		{
			uniq_->def_ = uniq_->lst_.at( uniq_->lst_.count()-1 );
		}
	}
	uniq_->lst_.removeAt( i );
}
/* ========================================================================= */

///* ------------------------------------------------------------------------- */
//BinDrawFile *	FormatManager::bldInBinary			( void )
//{
//	return static_cast<BinDrawFile *>( uniq_->lst_.at( 0 ) );
//}
///* ========================================================================= */

///* ------------------------------------------------------------------------- */
//SqlDrawFile *	FormatManager::bldInSQL				( void )
//{
//	return static_cast<SqlDrawFile *>( uniq_->lst_.at( 1 ) );
//}
///* ========================================================================= */

///* ------------------------------------------------------------------------- */
//XmlDrawFile *	FormatManager::bldInXML				( void )
//{
//	return static_cast<XmlDrawFile *>( uniq_->lst_.at( 2 ) );
//}
///* ========================================================================= */


/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
